/*******************************************************************************
*  Project      : Minigate Golfin                                 
*  Program ID   : pop_myreserve.js
*  Description  : 나의 예약내역 
*
********************************************************************************
*  Program History                                                            
*  Date        Author    Description                                          
*  ----------  --------  -------------------------------------------------------- 
*  2012-05-08  김효순    Created.  
*  2012-05-08  김효순    Complete.  
*******************************************************************************/

jQuery(function($){
	// 리스트
	function clsReserveList()
	{
		this._HTMLDom = null;
		this.listData = null;
		
		this._init();
	}
	
	clsReserveList.prototype = {
		_init: function(){
			var $layer = $("div.pw_area03 div.list_scroll tbody");
			this._HTMLDom = {
				"$layer": $layer
				, "$element": $layer.find("tr:first").css("cursor", "pointer").clone(true)
			}
			
			$layer.empty();
		}
		, $getLayer: function(){
			return this._HTMLDom["$layer"];
		}
		, setList: function(thisData){
			this.listData = thisData;
			this.display();
		}
		, display: function(){
			var $layer = this._HTMLDom["$layer"];
			var $element = this._HTMLDom["$element"];
			var thisClass = this;
			
			var noData = '<tr><td colspan="7" class="board_list_none">++ 예약중인 나의 예약내역이 없습니다. ++</td></tr>';
			
			
			$layer.empty();
			if(this.listData.length){
				var sValue = {
					"W": "예약대기"
					, "P": "가예약"
					, "G": "보증예약"
					, "A": "완료"
					//, "C": "취소"
					, "PC": "가예약취소"
					, "GC": "보증예약취소"
				}
				for(var i=0;i<this.listData.length;i++){
					$layer.append($element.clone(true));
				}
				$layer.find("tr").each(function(n){
					var d = thisClass.listData[n];
					
					$(this).attr("class", "");
					
					d["가격"] = d["가격"] || d["원가"];
					
					$(this).find("td:eq(0)").html(d["날짜"]);
					$(this).find("td:eq(1)").html(d["시간"]);
					$(this).find("td:eq(2)").html(d["골프장명"] + " " + d["코스명"]);
					$(this).find("td:eq(3)").html(d["홀"]);
					$(this).find("td:eq(4) p:eq(0)").html(d["가격"]);
					$(this).find("td:eq(4) p:eq(1)").html(d["원가"]);
					
					if(d["가격"] == d["원가"]) $(this).find("td:eq(4) p:eq(1)").hide();
					
					$layer.find("ul.ico_list img").each(function(n){
						var s = $(this).attr("src");
						$(this).attr("src", s.replace("_off", "").replace(".png", "_off.png"));
						if(d["이벤트"]){
							if(d["이벤트"].indexOf($(this).attr("alt")) != -1) $(this).attr("src", s.replace("_off", ""));
						}
					});
					
					$(this).find("td:eq(6)").html(sValue[d["상태코드"]] + ((d["상태코드"] == "P") ? "<br>(보증결제대기)":""));
				}).hover(function(){
					$(this).addClass("tr_on");
				}
				, function(){
					$(this).removeClass("tr_on");
				});
			}else{
				$layer.append(noData);
			}
		}
		, spinStart: function(){
			var $layer = this._HTMLDom["$layer"];
			
			if($layer.parent().parent().find("div#tmpSpin").length == 0){
				$layer.parent().parent().append("<div id='tmpSpin' style='position:relative; width:700px; height:100px;' />");
			}
			$layer.parent().parent().find("div#tmpSpin").show().spin();
		}
		, spinEnd: function(){
			var $layer = this._HTMLDom["$layer"];
			
			$layer.parent().parent().find("div#tmpSpin").hide().spin(false);
		}
		, getKey: function(n){
			return this.listData[n]["키"];
		}
		, getAreaKey: function(n){
			return this.listData[n]["지역코드"];
		}
		, getGolfKey: function(n){
			return this.listData[n]["골프장코드"];
		}
		, getCourseKey: function(n){
			return this.listData[n]["코스코드"];
		}
	}
	
	function clsComm()
	{
		this.reserveList = null;
		
		this._init();
	}
	
	clsComm.prototype = {
		_init: function(){
			
		}
		, getReserveList: function(){
			var thisClass = this;
			
			//ajax
			var serviceName = "getMyReservList";
			var options = {
				
			}
			_gf.ajax({
				"sid": "RS001"
				, "servicename": serviceName
				, "listcount": 100000
				, "currpageno": 1
			}, options, function(ret){
				var res = {
					"결과코드": ret["msp"]["contents"]["serviceResult"]
					, "메시지": document.objGlobal.getResultMsg(ret)
					, "결과값": []
				}
				
				var items = ret["msp"]["contents"]["items"];
				if(items){
					if(!items.length) items = new Array(items);
					for(var i=0;i<items.length;i++){
						var obj = items[i];
						var reserve_date = _util.nvl(obj["reserv__date"])+"";
						var reserve_time = _util.nvl(obj["reserv__time"])+"";
						
						var szEvent = "";
					
						if(obj["greenfee__use__yn"] == "Y"){
							szEvent = "그린피";
						}
						
						if(obj["cartfee__use__yn"] == "Y"){
							if(szEvent) szEvent += ",";
							szEvent += "카트피";
						}
						
						if(obj["food__use__yn"] == "Y"){
							if(szEvent) szEvent += ",";
							szEvent += "식사";
						}
						
						if(obj["present__use__yn"] == "Y"){
							if(szEvent) szEvent += ",";
							szEvent += "선물";
						}
						
						if(obj["present__use__yn"] == "Y"){
							if(szEvent) szEvent += ",";
							szEvent += "선물";
						}
						
						if(obj["etc__use__yn"] == "Y"){
							if(szEvent) szEvent += ",";
							szEvent += "기타";
						}
						
						res["결과값"].push({
							"키": _util.nvl(obj["reserv__seq"])
							, "날짜": reserve_date.substrEx(0, 4) + "." + reserve_date.substrEx(4, 2) + "." + reserve_date.substrEx(6, 2)
							, "시간": reserve_date.substrEx(0, 2) + ":" + reserve_date.substrEx(2, 2)
							, "골프장명": _util.nvl(obj["golfclub__name"])
							, "코스명": _util.nvl(obj["course__info__id1"])
							, "홀": _util.nvl(obj["hole"])
							, "가격": _util.nvl(obj["greenfee"], 0)
							, "원가": _util.nvl(obj["price"], 0)
							, "이벤트": szEvent
							, "상태코드": obj["reserv__status"]
						});
					}
				}
				
				document.objControl.finishReserveList(res);
			});
		}
		, getReserveData: function(){
			return this.reserveList;
		}
	}
	
	
	// 컨트롤
	function clsControl()
	{
		this.objComm = null;
		this.objReserveList = null;
		
		this._init();
	}
	
	clsControl.prototype = {
		_init: function(){
			this.objComm = new clsComm();
			this.objReserveList = new clsReserveList();
			
			this._bind();
			
			//예약데이터
			this.objReserveList.spinStart();
			this.objComm.getReserveList();
			
			document.objUtil.setWindowSize();
		}
		, _bind: function(){
			var thisClass = this;
			
			this.objReserveList.$getLayer().on("click", "tr", function(){
				var n = thisClass.objReserveList.$getLayer().find("tr").index($(this));
				var k = thisClass.objReserveList.getKey(n);
				var a = thisClass.objReserveList.getAreaKey(n);
				var g = thisClass.objReserveList.getGolfKey(n);
				var c = thisClass.objReserveList.getCourseKey(n);
				
				thisClass.goReserveViewPage(k, a, g, c);
			});
			
			// 종료
			$("p.pop_close, div.pw_area03 p.pop_bt02 a").click(function(){
				self.close();
			});
			
			$("div.pw_area03 p.reserve_txt03 span").css("cursor", "pointer");
			$("div.pw_area03 p.reserve_txt03").on("click", "span", function(){
				var t = $(this).text();
				
				switch(t){
					case "마이 골프인":
						thisClass.goMygolfinPage();
						break;
					case "부킹정보":
						thisClass.goBookinPage();
						break;
				}
			});
		}
		// 예약데이터 완료
		, finishReserveList: function(ret){
			if(ret["메시지"]) alert(ret["메시지"]);
			if(ret["결과코드"] != "00000") return;
			
			this.objReserveList.spinEnd();
			this.objReserveList.setList(ret["결과값"]);
		}
		// 마이골프인 이동
		, goMygolfinPage: function(){
			var szLink = "/mypage/my_home.html";
			
			$("body").append("<a id='idPermissionLink' href='" + szLink + "' target='_blank' />");
			$("a:last").get(0).click();
			$("a:last").remove();
		}
		// 부킹정보 이동
		, goBookinPage: function(){
			var szLink = "/mypage/my_booking.html";
			
			$("body").append("<a id='idPermissionLink' href='" + szLink + "' target='_blank' />");
			$("a:last").get(0).click();
			$("a:last").remove();
		}
		// 예약 상세 이동
		, goReserveViewPage: function(seq, area, golf, course){
			var param = {
				"q_reserve": seq
				, "q_area": area
				, "q_golf": golf
				, "q_course": course
			}
			
			
			var szLink = _ROOT + "/booking/reserve_view.html" + _util.object2paramString(param);
			$("body").append("<a href='" + szLink + "' id='idPermissionLink' target='_blank'></a>");
			$("a:last").get(0).click();
			$("a:last").remove();
		}
	}
	
	document.objControl = new clsControl();
});